package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.BussTypeDao;
import com.baichang.taoshunda.entity.BussType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class BussTypeDaoImpl extends BaseDaoImpl<BussType> implements BussTypeDao {
    /**
     * 首页获取商家分类
     *
     * @param cityId 城市编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getHomePageType(Integer cityId) {
//        AND `city_id` = ?
        String sql = "SELECT * FROM `t_buss_type` WHERE `state` = 1  ORDER BY rank DESC ";
        return queryForMapList(sql);
    }

    /**
     * 查询首页分类
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryHomePag(String name, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT bt.* FROM t_buss_type bt  WHERE bt.state IN (1) ";
        if (StringUtils.isNotBlank(name)) {
            sql += " AND bt.type_name LIKE ?";
            list.add("%" + name + "%");
        }
        sql += " ORDER BY bt.rank DESC ";
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateState(Integer id, Integer state) {
        String sql = "UPDATE {table} SET state=? WHERE id=?";
        return update(sql, state, id);
    }

    /**
     * 修改首页分类
     *
     * @param map
     * @param id
     * @return
     */
    @Override
    public int update(Map map, Integer id) {
        String sql = "id=?";
        return update(sql, map, id);
    }

    /**
     * 获取分类信息
     */
    @Override
    public List<Map<String, Object>> queryByTypeId() {
        String sql = "SELECT * FROM {table} WHERE state IN (1)";
        return queryForMapList(sql);
    }

    /**
     * 获取所有名称
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getTypeName() {
        String sql = "SELECT type_name AS name FROM {table} WHERE state IN (1)";
        return queryForMapList(sql);
    }

    /**
     * 修改排名
     *
     * @param id
     * @param rank
     * @return
     */
    @Override
    public int updateRank(Integer id, Integer rank) {
        String sql = "UPDATE {table} SET rank=? WHERE id=?";
        return update(sql, rank, id);
    }

    @Override
    public List<Map<String, Object>> getCompanyType() {
        String sql = "SELECT * FROM `t_buss_type` WHERE `state` = 1 and id!=57 ORDER BY rank DESC ";
        return queryForMapList(sql);
    }
}